// pages/list/list.js
var http = require('../../utils/rquest.js')
Page({
  /**
   * 页面的初始数据
   */
  data: {
    arr: [],
    link: '',
    msg: '',
    taskType: '',
    disab: true,
    current: 1,
    total: 0
  },
  //领取任务事件
  getTask(e) {
    let userRole = ''
  
    if (wx.getStorageSync("userMsg").roleList == "role_admin" || wx.getStorageSync("userMsg").roleList == "role_superAdmin") {
      wx.showToast({
        title: '您不能领取',
      })
      return
    }

    if (wx.getStorageSync('userMsg').roleList) {
      userRole = wx.getStorageSync('userMsg').roleList
    }
    if (userRole.charAt(userRole.length - 1) != this.data.taskType.charAt(this.data.taskType.length - 1)) {
      wx.showToast({
        title: '等级不足',
      })
      return
    }
    
    let obj = {
      taskId: e.currentTarget.dataset.item.id,
      creatorId: wx.getStorageSync('userMsg').id
    }
    http.accept(obj)
      .then(res => {
        if (res.data.status == "fail") {
          wx.showToast({
            title: res.data.msg, // 标题
            icon: 'success', // 图标类型，默认success
            duration: 2000 // 提示窗停留时间，默认1500ms
          })
          return
        }

        if (res.data.sysTask) {
          this.updata(res.data.sysTask)
        }
      })
      .catch(err => {
      })
  },
  //获取数据列表
  getAll(options) {
    http.getAll({
        taskType: this.data.taskType,
        current: this.data.current
      })
      .then(res => {
        if (res.statusCode == 200 && res.data.msg.list instanceof Array && res.data.msg.list.length > 0) {
          let arrlist = res.data.msg.list
          let newArr = []
          let totalPage = Math.ceil(res.data.msg.total / res.data.msg.pageSize)
          //修改数据值
          arrlist.map(function(item) {
            if (item.fileUids && item.fileUids.length > 0) {
              newArr = item.fileUids.split(',')
            }
            let taskType = ''
            newArr.shift()
            newArr.pop()
            item.fileUids = newArr
            switch (item.taskType) {
              case 'task_type_1':
                taskType = '普通任务'
                break;
              case 'task_type_2':
                taskType = '中级任务'
                break;
              case 'task_type_3':
                taskType = '高级任务'
                break;
            }
            item.taskType = taskType
          })

          // 翻页时是否合并数组的判断
          if (this.data.current == 1) {
            this.setData({
              arr: arrlist
            })
          } else {
            if (options === "缓存") {
              this.setData({
                arr: this.data.arr
              })
            } else {
              this.setData({
                arr: this.data.arr.concat(arrlist)
              })
            }
          }
          this.setData({
            total: totalPage
          })
        } else {
          wx.showToast({
            title: res.data.msg,
          })
          wx.hideLoading()
        }
      })
  },

  //修改缓存
  updata(row) {
    let arrdata = this.data.arr;
    let obj = row
    let imgs = obj.fileUids.split(',')
    imgs.pop()
    imgs.shift()
    obj.fileUids = imgs

    for (var i = 0; i < arrdata.length; i++) {
      if (arrdata[i].id === obj.id) {
        arrdata.splice(i, 1, obj)
      }
    }
    this.setData({
      arr: arrdata
    })
    this.getAll("缓存")
  },
  //预览图片
  previewImg(e) {
    let arr = e.currentTarget.dataset.imglist.map(function(item) {
      return 'https://app.yueluo.club/file/5gmaiq/' + item
    })
    wx.previewImage({
      current: e.currentTarget.dataset.flag, // 当前显示图片的http链接
      urls: arr // 需要预览的图片http链接列表
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    wx.showLoading({
      title: "加载中",
      mask: true
    })
    this.setData({
      taskType: options.taskType
    })
    this.getAll()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    //  wx.hideNavigationBarLoading();//完成加载
    wx.showLoading({
      title: '加载中',
      icon: 'loading'
    })
    this.setData({
      current: 1
    })
    this.getAll()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    // wx.showLoading({
    //   title: '加载中',
    //   icon: 'loading'
    // })
    let currentPage = this.data.current
    if (currentPage === this.data.total + 1) { //判断当前页超过总页数时候
      currentPage = this.data.total
      this.getAll()
    } else {
      currentPage++
      this.setData({
        current: currentPage
      })
      this.getAll()
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})